<template>
  <!-- 添加材料对话框 -->
  <el-dialog v-model="visible" title="添加施工明细" width="400px" @close="close" destroy-on-close>
    <el-form ref="commodityAddRef" :model="commodityAdd" :rules="rules" label-position="right" label-width="100px">

      <el-row>
        <el-form-item label="项目名" prop="contractId">
          <el-select placeholder="请选择项目" v-model="commodityAdd.contractId" clearable @change="changeContract">
            <el-option v-for="c of contract" :label="c.projectName" :value="c.contractId" :key="c.contractId"></el-option>
          </el-select>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item label="施工位置" prop="locationId">
          <el-select placeholder="请选择位置" v-model="locationId" 
          allow-create 
          clearable 
          @change="changeLocation" 
          filterable 
          :reserve-keyword="false">
            <el-option v-for="t of location" :label="t.locationName" :value="t.id" :key="t.id"></el-option>
          </el-select>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item label="上报日期" prop="date">
          <el-date-picker v-model="commodityAdd.date" type="date" placeholder="请选择日期" :size="size" format="YYYY-MM-DD" 
          value-format="YYYY-MM-DD"/>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item label="施工类型" prop="constructionTypeId">
          <el-select placeholder="请选择类型" v-model="commodityAdd.constructionTypeId" clearable @change="changeType">
            <el-option v-for="t of type" :label="t.constructionType" :value="t.id" :key="t.id"></el-option>
          </el-select>
        </el-form-item>
      </el-row>

      <!-- 计量数 -->
      <el-row>
        <el-form-item :label="newType.measurement + '(' + newType.unitName + ')'" prop="number">
          <el-input v-model="commodityAdd.number" />
        </el-form-item>
      </el-row>

      <div v-if="showType">
        <el-row>
          <el-form-item label="所用产品：" prop="productId">
            <el-select placeholder="请选择产品" v-model="commodityAdd.productId" clearable>
              <el-option v-for="store of stores" :label="store.productName" :value="store.productId"
                :key="store.productId"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="newType.measurementProduct + '(' + newType.unitProductName + ')'" prop="numberProduct">
            <el-input v-model="commodityAdd.numberProduct" />
          </el-form-item>
        </el-row>
      </div>

      <el-row>
        <el-form-item label="天气" prop="weather">
          <el-input v-model="commodityAdd.weather" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="commodityAdd.remark" />
        </el-form-item>
      </el-row>

    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="addCommodity">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { post, tip, WAREHOUSE_CONTEXT_PATH, get } from "@/common"
import { Plus } from '@element-plus/icons-vue'

// 该页面的可见性
const visible = ref(false);

// 添加材料对象
const commodityAdd = reactive({
  dailyConstructionId: '',
  date: '',
  contractId: '',
  locationId: '',
  newLocationName: '',
  constructionTypeId: '',
  productId: '',
  weather: '',
  number: '',
  numberProduct: '',
  remark: '',
});

const locationId = ref([])

// 表单验证规则
const rules = reactive({
  constructionType: [
    { required: true, message: '请输入类型名称', trigger: 'blur' }
  ],
  measurement: [
    { required: true, message: '请输入计量名称', trigger: 'blur' }
  ],
  unit: [
    { required: true, message: '请选择单位', trigger: 'blur' }
  ],
  // measurementProduct: [
  //   { required: true, message: '请输入产品计量名称', trigger: 'blur' }
  // ],
  // unitProduct: [
  //   { required: true, message: '请选择产品单位', trigger: 'blur' }
  // ],
})

// 关闭
const close = () => {
  for (let prop in commodityAdd) {
    commodityAdd[prop] = '';
  }
  visible.value = false;
}

// 所有产品
const stores = ref();
// 所有类型
const type = ref();
// 所有合同
const contract = ref();
// 所有位置
const location = ref();
// 所有单位
const units = ref();

// 获得所有合同
const getContract = () => {
  get("/contract/contract-all").then(result => {
    contract.value = result.data;
  })
};
// 获得所有位置
const getLocation = () => {
  get("/location/location-list/" + commodityAdd.contractId).then(result => {
    location.value = result.data;
  })
};
// 获得所有类型
const getType = () => {
  get("/constructionType/constructionType-list/" + commodityAdd.contractId).then(result => {
    type.value = result.data;
  })
  if (type.length < 1) {
    newType.value = {
      measurement: "无施工类型",
      unitProductName: "无施工类型"
    }
  } else {
    newType.value = type.value[0];
  }
};


// 树形结构数据对应实体类属性
const defaultProps = {
  children: 'childProductCategory',
  label: 'typeName',
};

// 该对话框打开，进行数据初始化
const open = (storeList, unitList, typeList) => {
  getContract();
  // getLocation();
  console.log(typeList.value)
  visible.value = true;
  stores.value = storeList.value;
  units.value = unitList.value;
  type.value = typeList.value;
  if (typeList.length < 1) {
    newType.value = {
      measurement: "无施工类型",
      unitProductName: "无施工类型"
    }
  } else {
    console.log(type)
    newType.value = type.value[0];
  }
};

const commodityAddRef = ref();
// 定义
const emit = defineEmits(["ok"]);
// 添加材料提交
const addCommodity = () => {

  if(typeof locationId.value === 'number'){
    commodityAdd.locationId = locationId.value
  }else{
    commodityAdd.newLocationName = locationId.value
  }
  commodityAddRef.value.validate(valid => {
    if (valid) {
      post('/construction/add', commodityAdd).then(result => {
        emit('ok');
        tip.success(result.message);
        visible.value = false; // 关闭对话框
      });
    }
  });
};

//当前类型
const newType = ref();
// 产品输入是否显示
const showType = ref();
// 选择施工类型，改变输入
const changeType = () => {
  newType.value = type.value.find(t => t.id == commodityAdd.constructionTypeId)
  if (newType.value.productId != null) {
    showType.value = true;
  } else {
    showType.value = false;
  }
}

// 选择项目
const changeContract = () => {
  console.log("changeContract")
  getLocation();
  getType();
}

const changeLocation = () => {
  console.log(locationId.value)
  console.log(location.value)
}


defineExpose({ open });
</script>

<style scoped>
.avatar-uploader,
.avatar-uploader .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>